View Javadoc
1   package edu.jiangxin.apktoolbox.swing.extend.plugin.runnable;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.plugin.ChangeMenuPreparePluginController;
4   import edu.jiangxin.apktoolbox.swing.extend.plugin.IPreparePluginCallback;
5   import net.lingala.zip4j.ZipFile;
6   import net.lingala.zip4j.progress.ProgressMonitor;
7   import org.apache.commons.io.FileUtils;
8   import org.apache.logging.log4j.LogManager;
9   import org.apache.logging.log4j.Logger;
10  
11  import javax.swing.*;
12  import java.io.File;
13  import java.io.IOException;
14  
15  public class UnzipRunnable extends AbstractRunnable {
16      private static final Logger LOGGER = LogManager.getLogger(UnzipRunnable.class.getSimpleName());
17      private final File pluginFile;
18  
19      public UnzipRunnable(File pluginFile, IPreparePluginCallback callback) {
20          super("Unzipping...", callback);
21          this.pluginFile = pluginFile;
22      }
23  
24      @Override
25      public void run() {
26          SwingUtilities.invokeLater(() -> progressBarDialog.setVisible(true));
27          LOGGER.info("Unzip file: {}", pluginFile);
28          String parentDir = pluginFile.getParent();
29          try (ZipFile zipFile = new ZipFile(pluginFile)) {
30              zipFile.setRunInThread(true);
31              zipFile.extractAll(parentDir);
32              while (zipFile.getProgressMonitor().getState() == ProgressMonitor.State.BUSY) {
33                  if (isCancelled) {
34                      zipFile.getProgressMonitor().setCancelAllTasks(true);
35                      break;
36                  }
37                  progress = zipFile.getProgressMonitor().getPercentDone();
38                  Thread.sleep(100);
39              }
40          } catch (IOException e) {
41              LOGGER.error("unzipFile failed: IOException");
42              callback.onUnzipFinished(ChangeMenuPreparePluginController.RESULT_UNZIP_FAILED);
43              return;
44          } catch (InterruptedException e) {
45              LOGGER.error("unzipFile failed: InterruptedException");
46              callback.onUnzipFinished(ChangeMenuPreparePluginController.RESULT_UNZIP_FAILED);
47              Thread.currentThread().interrupt();
48          }
49  
50          if (isCancelled) {
51              LOGGER.info("download cancelled");
52              String fileName = pluginFile.getAbsolutePath();
53              String dirName = fileName.substring(0, fileName.lastIndexOf("."));
54              FileUtils.deleteQuietly(new File(dirName));
55              callback.onUnzipFinished(ChangeMenuPreparePluginController.RESULT_UNZIP_CANCELLED);
56          } else {
57              isFinished = true;
58              callback.onUnzipFinished(ChangeMenuPreparePluginController.RESULT_UNZIP_SUCCESS);
59          }
60      }
61  }